import * as THREE from 'three';
import { OrbitControls } from 'three/examples/jsm/controls/OrbitControls';

export const useVRControls = (sceneObjects) => {
  const { scene, camera, renderer } = sceneObjects;
  const moveSpeed = 0.1;
//   const rotateSpeed = 0.02;
  
  // 创建轨道控制器（用于非VR模式下的视角控制）
  const orbitControls = new OrbitControls(camera, renderer.domElement);
  orbitControls.enableDamping = true;
  orbitControls.dampingFactor = 0.05;
  
  // 移动状态
  const moveState = {
    forward: false,
    backward: false,
    left: false,
    right: false,
    up: false,
    down: false,
  };

  // 键盘控制
  const handleKeyDown = (event) => {
    switch (event.code) {
      case 'KeyW': moveState.forward = true; break;
      case 'KeyS': moveState.backward = true; break;
      case 'KeyA': moveState.left = true; break;
      case 'KeyD': moveState.right = true; break;
      case 'Space': moveState.up = true; break;
      case 'ShiftLeft': moveState.down = true; break;
    }
  };

  const handleKeyUp = (event) => {
    switch (event.code) {
      case 'KeyW': moveState.forward = false; break;
      case 'KeyS': moveState.backward = false; break;
      case 'KeyA': moveState.left = false; break;
      case 'KeyD': moveState.right = false; break;
      case 'Space': moveState.up = false; break;
      case 'ShiftLeft': moveState.down = false; break;
    }
  };

  // VR控制器
  const controller1 = renderer.xr.getController(0);
  const controller2 = renderer.xr.getController(1);
  
  scene.add(controller1);
  scene.add(controller2);

  // 添加控制器模型（用于在VR中显示控制器）
  const controllerGeometry = new THREE.BoxGeometry(0.02, 0.02, 0.1);
  const controllerMaterial = new THREE.MeshStandardMaterial({
    color: 0xff0000
  });
  
  const controller1Mesh = new THREE.Mesh(controllerGeometry, controllerMaterial);
  const controller2Mesh = new THREE.Mesh(controllerGeometry, controllerMaterial);
  controller1.add(controller1Mesh);
  controller2.add(controller2Mesh);
  
  // VR控制器事件
  controller1.addEventListener('selectstart', onSelectStart);
  controller1.addEventListener('selectend', onSelectEnd);
  controller2.addEventListener('selectstart', onSelectStart);
  controller2.addEventListener('selectend', onSelectEnd);
  
  function onSelectStart() {
    this.userData.isSelecting = true;
  }
  
  function onSelectEnd() {
    this.userData.isSelecting = false;
  }

  // 更新函数
  const update = () => {
    // 键盘移动控制
    if (moveState.forward) camera.translateZ(-moveSpeed);
    if (moveState.backward) camera.translateZ(moveSpeed);
    if (moveState.left) camera.translateX(-moveSpeed);
    if (moveState.right) camera.translateX(moveSpeed);
    if (moveState.up) camera.translateY(moveSpeed);
    if (moveState.down) camera.translateY(-moveSpeed);

    // VR控制器移动
    if (controller1.userData.isSelecting) {
      camera.translateZ(-moveSpeed);
    }
    if (controller2.userData.isSelecting) {
      camera.translateZ(moveSpeed);
    }

    // 更新轨道控制器
    orbitControls.update();
  };

  // 添加事件监听
  window.addEventListener('keydown', handleKeyDown);
  window.addEventListener('keyup', handleKeyUp);
  
  return {
    update,
    dispose: () => {
      window.removeEventListener('keydown', handleKeyDown);
      window.removeEventListener('keyup', handleKeyUp);
      controller1.removeEventListener('selectstart', onSelectStart);
      controller1.removeEventListener('selectend', onSelectEnd);
      controller2.removeEventListener('selectstart', onSelectStart);
      controller2.removeEventListener('selectend', onSelectEnd);
      scene.remove(controller1);
      scene.remove(controller2);
      orbitControls.dispose();
    }
  };
}; 